<?php

require_once 'include/language_manager.inc.php';
Language_Manager::getInstance();

$meta["title"] = L::meta_vips_title;
$meta["description"] = L::meta_vips_description;
$meta["keywords"] = L::meta_vips_keywords;

require "include/template_manager.inc.php";
require "include/image_manager.inc.php";
$myTemplate = new Template_Manager("lista_vip", null, true, 9, $meta);
$query = "SELECT COUNT(*) AS conta FROM vip";
$num = 10;
if ($data = DB_Manager::getInstance()->query($query)) {
    $row = $data->fetch_array(MYSQLI_ASSOC);
    $last = (int) ($row['conta'] / $num);
}

$prefix = "/";
$labelVips = "vipincucina";
if (getLanguage() == 'en') {
    $prefix = "/en/";
    $labelVips = "vipsinkitchen";
}

if (!isset($_REQUEST['pagina']) || $_REQUEST['pagina'] == 1) {
    $myTemplate->setCanonicalUrl($prefix . "vip/");
    $pagina = 0;
} else {
    $myTemplate->setCanonicalUrl($prefix . "vip/" . $_REQUEST['pagina'] . "/");
    $pagina = (int) $_REQUEST['pagina'] - 1;
}
$start = $num * $pagina;
$query = "SELECT `v`.`Id` AS `id`, `v`.`titolo` AS `titolo`, `v`.`titolo_en` AS `titolo_en`,`v`.`vip` AS `vip`,"
        . "`v`.`Data` as `data`,`i`.`uri` as `uri`, concat(`p`.`nome`,' ',`p`.`cognome`) AS `autore`,"
        . "`p`.`titolo` AS `prenome`,`i`.`descrizione` AS `descrizione`,`i`.`descrizione_en` AS `descrizione_en` "
        . "FROM ((`vip` `v` join `immagine` `i`) join `professori` `p`) "
        . "WHERE (`v`.`immagine` = `i`.`id`) and (`v`.`autore` = `p`.`id`) "
        . "ORDER BY `v`.`data` DESC LIMIT " . $start . ", " . $num;
if ($data = DB_Manager::getInstance()->query($query)) {
    while ($row = $data->fetch_array(MYSQLI_ASSOC)) {
        $myTemplate->setBodyContentString("id", $row['id']);
        if (getLanguage() == 'it') {
            $myTemplate->setBodyContentString("titolo", $row['titolo']);
            $myTemplate->setBodyContentString("link_titolo", Config::$site_name . $prefix . 'vip/' . $row['titolo'] . '/');
            $descrizione=$row['descrizione'];
        } else {
           $title = str_replace('-', "'", $row['titolo_en']);
           $myTemplate->setBodyContentString("titolo", $title);
           $myTemplate->setBodyContentString("link_titolo", Config::$site_name . $prefix . 'vip/' . $row['titolo_en'] . '/');
           $descrizione=$row['descrizione_en'];
        }
        $myTemplate->setBodyContentString("vip", $row['vip']);
        $myTemplate->setBodyContentString("autore", $row['autore']);
        $myTemplate->setBodyContentImage("link_immagine", $row['uri']);
        $myTemplate->setBodyContentImage("link_immagine_thumb", Image_Manager::thumb($row['uri']));
        $introlen = 200;
        $descr = (strlen($descrizione) > $introlen) ? substr($descrizione, 0, strpos($descrizione, ' ', $introlen)) . "..." : $descrizione;
        $myTemplate->setBodyContentString("descr", $descr);
    }
}
$range = 2;
$s = ($pagina > $range) ? $pagina - $range : 0;
$list = "";
if ($pagina > 0) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelVips . '/' . $pagina . '/">' . L::messages_vips_list_prev . '</a></li>';
}
for (; $s < $pagina; $s++) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelVips . '/' . ($s + 1) . '/">' . ($s + 1) . '</a></li>';
}
$list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelVips . '/' . ($s + 1) . '/" class="current">' . ($s + 1) . '</a></li>';
$s++;
$end = (($pagina + $range) > $last) ? $last + 1 : ($pagina + $range) + 1;
for (; $s < $end; $s++) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelVips . '/' . ($s + 1) . '/">' . ($s + 1) . '</a></li>';
}
if ($pagina < $last) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelVips . '/' . ($pagina + 1) . '/">' . L::messages_vips_list_next . '</a></li>';
}
$myTemplate->setBodyContentString("lista", $list);

$myTemplate->setBodyContentString("nav_message", L::nav_message);
$myTemplate->setBodyContentString("nav_home", L::nav_home);
$myTemplate->setBodyContentString("nav_page", L::messages_vips_nav_page);
$myTemplate->setBodyContentString("nav_title", L::messages_vips_nav_title);

$myTemplate->setBodyContentString("read_all", L::messages_vips_read_all);